home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / pixmaps.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  14.1 KB  |  403 lines

  1. #ifndef    lint
  2. static char SccsId[]= "@(#)pixmaps.c    V1.16    3/13/95";
  3. #endif
  4. /*
  5. |    file name - pixmaps.c
  6. |===================================================================
  7. |
  8. |    This program demonstrates the use of pixmaps, images and icons.
  9. |    It loads an existing view and adds an image and icon to the view;
  10. |    the icon is also given a simple mask.
  11. |     
  12. |    The image acts as a large background canvas, and the icon is drawn
  13. |    in front of it. The view contains an object named "dynamic.obj"
  14. |    whose dynamics are attached to the icon.
  15. |
  16. |    The images used in this example program are found in the
  17. |    <dataviews>/lib/images directory.
  18. |
  19. |    The programs ends if the user selects the <q|Q> key or the
  20. |    right mouse button.
  21. |
  22. |===================================================================
  23. */
  24.  
  25. /*
  26.  *  DV-Tools header files
  27.  */
  28. #include "std.h"           /* <stdio.h> etc., scalar & macro definitions */
  29. #include "dvstd.h"         /* public types & constants */
  30. #include "dvtools.h"       /* constants used by T routines */
  31. #include "dvGR.h"          /* constants used by window mgt & GR routines */
  32. #include "VOstd.h"         /* constants used by VO & VOob routines */
  33. #include "Tfundecl.h"      /* T routines (screens, drawports & views) */
  34. #include "VOfundecl.h"     /* VO routines (objects) */
  35. #include "VUerfundecl.h"   /* VUer routines (event handling routines) */
  36. #include "VUfundecl.h"     /* VU routines (utilities) */
  37.  
  38. /* Constants */
  39. #define  DVPATH            (char *)NULL
  40. #define  DISPFORMS_STB     (char *)NULL
  41. #define  DVDEVICE          (char *)NULL
  42. #define  DVCOLORTABLE      (char *)NULL
  43. #define  VIEW_NAME         "pixmaps.v"
  44. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  45.  
  46. /* Whole world rectangle, (-16384, -16384) to (16383, 16383)*/
  47. RECTANGLE whole_world = {XMIN, YMIN, XMAX, YMAX};
  48.  
  49. /* Functions defined in pixmaps.c */
  50.  
  51. /*
  52.  *   MAIN PROGRAM
  53.  */
  54. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  55.                      LPSTR lpCmdLine,  int nCmdShow  )
  56. {
  57.   /*
  58.    *  program arguments
  59.    *    argv[1] - display device name (default is to use DVDEVICE)
  60.    */
  61.  
  62.   /* Define & initialize device name and view filename */
  63.   char *device_name = DVDEVICE; /* default device name */
  64.   char *view_name = VIEW_NAME;  /* default view name */
  65.  
  66.   /* Define display variables */
  67.   OBJECT   screen;       /* display device, the window */
  68.   DRAWPORT drawport;     /* how & where to display picture, picture frame */
  69.   VIEW     view;         /* picture representation of the view file */
  70.  
  71.   /* Control loop variables */
  72.   OBJECT location;              /* the event representation */
  73.   int event_req_status;         /* status of event requests */
  74.  
  75.   int argc = 0;
  76.   char **argv;
  77.   /* pixmap and image object variables */
  78.   OBJECT drawing,               /* graphical representation of screen */
  79.     p1, p2,                     /* control points used by objects */
  80.     pixmap,                     /* pixmap object */
  81.     back_image,                 /* image object */
  82.     fore_icon,                  /* icon object */
  83.     dyn_obj,                    /* dynamic control object */
  84.     dynamics;                   /* object with dynamics from main view */
  85.   ATTRIBUTES attr;              /* attributes structure */
  86.   COLOR_TABLE *colorp;  /* color table representing colors used by pixmap */
  87.   COLOR_XFORM color_xform;      /* color xform structure */
  88.  
  89.   /* Other variables */
  90.   int Quit = NO,                /* flag to quit program */
  91.       index;                    /* counter */
  92.  
  93.   /*--------------------
  94.    *   Initialization
  95.    *
  96.    *   TInit:  perform the initialization of DV-Tools
  97.    *           TInit reads your configuration file and any
  98.    *           environment variables or logical names set.
  99.    */
  100.   make_argv(&argc,&argv,GetCommandLine());
  101.   TInit( DVPATH, DISPFORMS_STB );
  102.  
  103.   /*
  104.    *   TscOpenSet: open a device as a screen object using
  105.    *               specified attributes
  106.    *
  107.    *   Set exposure block to YES to insure the window
  108.    *   is ready for drawing when TdpDraw is called.
  109.    */
  110.   if (argc > 1)
  111.     device_name = argv[1];
  112.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  113.                        V_X_EXPOSURE_BLOCK, YES,
  114.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  115.   if (!screen)
  116.     {
  117.       printf ("Must specify device on command line or");
  118.       printf (" in DataViews configuration file.\n");
  119.       S_EXIT (EXIT_ERR);
  120.     }
  121.  
  122.   /*
  123.    *   VOscWinEventMask:  sets the screen's window event mask
  124.    */
  125.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS |
  126.                             V_EXPOSE | V_RESIZE | V_MOTIONNOTIFY,
  127.             (ULONG) 0);
  128.  
  129.   /*
  130.    *   TviLoad:   Load a view in from a file
  131.    *   TviGetDrawing:  Gets a view's drawing object
  132.    */
  133.   view = TviLoad (view_name);
  134.   if (!view)
  135.     {
  136.       printf ("Could not load view from file ");
  137.       printf ("%s.\n", view_name);
  138.       S_EXIT (EXIT_ERR);
  139.     }
  140.   drawing = TviGetDrawing (view);
  141.  
  142.   /*
  143.    *   TdpCreateStretch: Create a drawport
  144.    *                "drawport" is attached to the screen object, "screen"
  145.    *                "view" is the view to be displayed on the screen
  146.    *                SCREEN_VIEWPORT specifies the screen viewport
  147.    *                "whole_world" specifies the portion of the view to be
  148.    *                      displayed, here the whole view
  149.    *                The whole view will be stretched to fit in the drawport
  150.    */
  151.   drawport = TdpCreateStretch (screen, view,
  152.                                SCREEN_VIEWPORT, &whole_world);
  153.  
  154.   /*
  155.    *  VOpmCreate: Create and returns a pixmap
  156.    */
  157.   pixmap = VOpmCreate ("monalisa.gif", (ADDRESS) NULL);
  158.  
  159.   /*
  160.    *  VOuAtInit:  Sets all attribute fields to EMPTY_FIELD.
  161.    *  VOptCreate: Create a point object
  162.    *
  163.    *  Initialize the attribute text position. This attribute
  164.    *  determines which point in the raster remains stationary
  165.    *  when the image is scaled.  Create a image object from
  166.    *  the pixmap specifying the control points identifying
  167.    *  it's location.
  168.    */
  169.   VOuAtInit (&attr);
  170.   attr.text_position = AT_BOTTOM_EDGE;
  171.   p1 = VOptCreate (WORLD_COORDINATES, -15000, 11000, (OBJECT) NULL);
  172.   p2 = VOptCreate (WORLD_COORDINATES, 15000, -8000, (OBJECT) NULL);
  173.   back_image = VOimCreate (pixmap, p1, p2, &attr, V_IM_ATTR_ARGEND);
  174.  
  175.   /*
  176.    *  VOdrObAdd:  Adds an object to the drawing
  177.    */
  178.   VOdrObAdd (drawing, back_image);
  179.  
  180.   /*
  181.    *  VOimScalePixmap:  Displays an image at an exact scale factor
  182.    *
  183.    *  Now adjust background so it is scaled exactly one pixmap pixel
  184.    *  per screen pixel. Thus it will look right at the current zoom factor,
  185.    *  but, being an image, will still change size if the drawport is zoomed.
  186.    *  Since the image's text position attribute is AT_BOTTOM_EDGE, if the
  187.    *  image expands it will do so along the top and side edges. The lower
  188.    *  edge stays fixed, and so can't overwrite the message or slider
  189.    *  that are under the image.
  190.    */
  191.   VOimScalePixmap (back_image, TdpGetXform (drawport, DR_TO_SCREEN),
  192.                    1.0, 1.0);
  193.  
  194.   /*
  195.    *  VOpmCreate: Create and returns a pixmap
  196.    *  VOicCreate: Create an icon from a pixmap
  197.    *
  198.    *  Create an icon object from the pixmap. An icon object displays a
  199.    *  raster which stays a fixed size on the screen even if the drawport
  200.    *  containing the object is zoomed.  Therefore, the icon object only
  201.    *  specifies an single anchor point.
  202.    */
  203.   pixmap = VOpmCreate ("rose.gif", (ADDRESS) NULL);
  204.   fore_icon = VOicCreate (pixmap, p1, (ATTRIBUTES *) NULL, V_IC_ATTR_ARGEND);
  205.  
  206.   /*
  207.    *  We now create a simple mask for this icon.
  208.    *  This will be done by using the icon's pixmap as the mask and using
  209.    *  a color transform to map all the colors in this pixmap to 1 except
  210.    *  a few colors. This works OK for "rose.gif" as most of the
  211.    *  background pixels use one of a few dark greys, and these greys are
  212.    *  used only as background. For other icons, more complex mask pixmaps
  213.    *  and/or color transforms might be more appropriate.
  214.    */
  215.  
  216.   /*
  217.    *  VOpmGet:  Get information about a pixmap
  218.    *
  219.    *  Get the colors used by the pixmap.
  220.    */
  221.   VOpmGet (pixmap, V_PM_COLOR_TABLE, &colorp, V_PM_ATTR_ARGEND);
  222.  
  223.   /*
  224.    *  Loop through all the colors in the color table obtained from
  225.    *  the pixmap and set all the indices in the color xform structure
  226.    *  to 1. This makes all colors visible. This color xform structure
  227.    *  will be used to set the mapping of the pixmap's color indicies
  228.    *  to the screen's color indices.
  229.    */
  230.   color_xform.size = colorp->ctsize;
  231.   for (index = 0; index < colorp->ctsize; index++)
  232.     color_xform.new_index[index] = 1;
  233.  
  234.   /*
  235.    *  VUctRGBtoIndex:  Finds the closest match to a color in a color table.
  236.    *
  237.    *  Find the index of the background color and make it transparent.
  238.    *  For "rose.gif" the colors (16,16,16), (16,8,16), (16,16,8), (16,8,8)
  239.    *  are used for most of the background pixels. Set the index in the
  240.    *  color xform structure to 0 representing invisible.
  241.    */
  242.   VUctRGBtoIndex (colorp, 16, 16, 16, &index);
  243.   color_xform.new_index[index] = 0;
  244.   VUctRGBtoIndex (colorp, 16, 16, 8, &index);
  245.   color_xform.new_index[index] = 0;
  246.   VUctRGBtoIndex (colorp, 16, 8, 16, &index);
  247.   color_xform.new_index[index] = 0;
  248.   VUctRGBtoIndex (colorp, 16, 8, 8, &index);
  249.   color_xform.new_index[index] = 0;
  250.  
  251.   /*
  252.    *  VOicSet:  Set characteristics for an icon
  253.    *
  254.    *  V_IC_MASK_PIXMAP:       pixmap used as the writemask
  255.    *  V_IC_MASK_PIXMAP_XFORM: Mapping of the pixmap's color indices
  256.    */
  257. //  VOicSet (fore_icon, V_IC_MASK_PIXMAP, pixmap,
  258. //           V_IC_MASK_PIXMAP_XFORM, &color_xform, V_IC_ATTR_ARGEND);
  259.  
  260.   /*
  261.    *  VOobDyGet:      Returns the dynamic control object
  262.    *              attached to the object
  263.    *  VOobDySet:      Associates a dynamic control bject with
  264.    *              a graphical object
  265.    *  VOdrObReplace:  Replaces current object with new object
  266.    *  VOdrObTop:      Moves an object to the top of the drawing
  267.    *
  268.    *  Obtain the named object, "dynamic.obj" from the drawing. Get
  269.    *  this object's dynamic control object.  Then associate this
  270.    *  dynamic control object with the created icon object. Replace
  271.    *  the icon object with the object from the drawing and move
  272.    *  the icon object to the top of the drawing list.
  273.    */
  274.   dyn_obj = TdrGetNamedObject (drawing, "dynamic.obj");
  275.   dynamics = VOobDyGet (dyn_obj);
  276.   VOobDySet (fore_icon, dynamics);
  277.   VOdrObReplace (drawing, dyn_obj, fore_icon);
  278.   VOdrObTop (drawing, fore_icon);
  279.  
  280.   /*
  281.    *   TscErase: Erase the entire screen in the default
  282.    *             background color
  283.    *   TdpDraw:  Draw the contents of the drawport
  284.    */
  285.   TscErase (screen);
  286.   TdpDraw (drawport);
  287.  
  288.   /*--------------------
  289.    *   Control loop
  290.    *
  291.    *   Poll the event queue for window events specified by the
  292.    *   window mask.  Handle each of the events as they happen.
  293.    *   Events occurring within input objects will be handled
  294.    *   through the event request handler VUerHandleLocEvent.
  295.    *   Updates are performed if an input object used the
  296.    *   the event.
  297.    */
  298.   FOREVER
  299.   {
  300.     /*
  301.      *  VOloWinEventPoll:  Polls for the next window event.
  302.      *                     The polling mode used is V_WAIT.
  303.      *                     Therefore, VOloWinEventPoll does not
  304.      *                     return until a masked event is
  305.      *                     generated.  V_WAIT always produces
  306.      *                     a valid location object.
  307.      */
  308.     location = VOloWinEventPoll (V_WAIT);
  309.  
  310.     /*
  311.      *  VOloType:  returns the type of event.  These types
  312.      *             match event types specified in VOscWinEventMask.
  313.      */
  314.     switch (VOloType (location))
  315.       {
  316.  
  317.       case V_EXPOSE:
  318.         /*
  319.          *  VOloRegion:  Returns a rectangle representing the
  320.          *               exposed region on the screen.
  321.          *  TscRedraw:   After erasing, redraws all the drawports
  322.          *               in the screen.
  323.          *  A portion of the window has been exposed and needs
  324.          *  to be redrawn.
  325.          */
  326.         TscRedraw (screen, VOloRegion (location));
  327.         break;
  328.  
  329.       case V_RESIZE:
  330.         /*
  331.          *  The window size has been changed.
  332.          *  TscReset:  Resets all screen drawports after
  333.          *             window resizing
  334.          */
  335.         TscReset (screen);
  336.         break;
  337.  
  338.       case V_KEYPRESS:
  339.         /*
  340.          *  Check key selected.
  341.          *  VOloKeySym:  Returns the key symbol value of the
  342.          *               location object
  343.          *  If the key symbol represents the characters 'q'
  344.          *  or 'Q' then quit the program.
  345.          */
  346.         switch (VOloKeySym (location))
  347.           {
  348.           case 'q':
  349.           case 'Q':
  350.             Quit = YES;
  351.             break;
  352.  
  353.           default:
  354.             break;
  355.           }
  356.         break;
  357.  
  358.       case V_BUTTONPRESS:
  359.       case V_MOTIONNOTIFY:
  360.         /*
  361.          *  VUerHandleLocEvent: Service the event.
  362.          *                      This routine will check if the
  363.          *                      event is used by any input objects
  364.          *                      that are in the view.
  365.          */
  366.         event_req_status = VUerHandleLocEvent (location);
  367.  
  368.         /*
  369.          * Update view only if an input object used the event
  370.          * TdpDrawNext: Update all dynamic objects within a
  371.          *              drawport's view.
  372.          */
  373.         if (event_req_status != INPUT_UNUSED)
  374.           TdpDrawNext (drawport);
  375.         break;
  376.  
  377.       default:
  378.         break;
  379.       }
  380.  
  381.     /* exit the program */
  382.     if (Quit == YES)
  383.       break;
  384.   }
  385.  
  386.   /*--------------------
  387.    *   Termination
  388.    *
  389.    *   TscErase:     Erase the entire screen in the default
  390.    *                 background color
  391.    *   TdpDestroy:   Destroy the drawport,
  392.    *   TviDestroy:   Destroy the view, freeing the allocated memory
  393.    *   TscCloseCurrentScreen:  Close the current display screen
  394.    *   TTerminate:   Perform the clean-up for DV-Tools
  395.    */
  396.   TscErase (screen);
  397.   TdpDestroy (drawport);
  398.   TviDestroy (view);
  399.   TscCloseCurrentScreen ();
  400.   TTerminate ();
  401.   return EXIT_OK;
  402. }
  403.